<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        /** 给定 csv 文件，转换成对象结构，提供测试用例，并补充 typescript 函数签名
* interface Person {
*   name: string;
*   age: number;
*   parent: Person[];
*   children: Person[];
* }
*/

        const csv = `
name,age,parent
Bob,30,David
David,60,
Anna,10,Bob
`;

        const processor = (csv) => {
            const arr = [...csv.trim().split("\n")].filter((item, index) => index >= 1).map(item => item.split(","));
            console.log(arr);

            function recursion(arr, index) {
                let len = arr.length;

                for (var i = 0; i < len; i++) {
                    // console.log(index);
                    console.log(index, i, arr[index][2], arr[i][0]);
                    if (arr[i][0] === arr[index][2]) {
                        break;
                    }
                }


                for (var j = 0; j < len; j++) {
                    if (arr[j][2] === arr[index][0]) {
                        break;
                    }

                }
                // console.log(i, j);
                // console.log("--------");
                if (i < len && j < len) {
                    console.log(i, j, "i,j-------------");

                    return [{
                        name: arr[index][0],
                        age: arr[index][1],
                        parent: recursion(arr, i),
                        children: recursion(arr, j)
                    }]
                }
                else if (i > 0 && i < len && j === len) {
                    console.log(i, j, "i--------------");
                    return [{
                        name: arr[index][0],
                        age: arr[index][1],
                        parent: recursion(arr, i),
                        // children: recursion(arr, j)
                    }]
                }
                else if (j > 0 && j < len && i === len) {
                    console.log(i, j, "j--------");
                    return [{
                        name: arr[index][0],
                        age: arr[index][1],
                        // parent: recursion(arr, i),
                        children: recursion(arr, j)
                    }]
                }
                else {
                    console.log(i, j, "--------------");
                    return [
                        {
                            name: arr[index][0],
                            age: arr[index][1]
                        }
                    ]
                }
            }
            const json = recursion(arr, 0);
            console.log(json, "---");
        }
        processor(csv)

// 期望返回这样的数据结构
//[
//  {
//    name: 'Bob',
//    age: '30',
//    parent: [
//      {
//        name: 'David',
//        age: 60
//      }
//    ],
//    children: [
//      {
//        name: 'Anna',
//        age: 10,
//      }
//    ]
//  }
//]

    </script>
</body>

</html>